package com.heart.house.web.controller;

import com.heart.house.biz.service.CommentService;
import com.heart.house.common.model.Comment;
import com.heart.house.common.model.User;
import com.heart.house.common.utils.BeanHelper;
import com.heart.house.web.interceptor.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author Luo Zhen
 * @create 2019-06-28 11:59
 */
@Controller
public class CommentController {

    @Autowired
    private CommentService commentService;

    @RequestMapping("/comment/leaveComment")
    public String leaveComment(Long houseId, Integer type, String content) {
        User user = UserContext.getUser();
        Comment comment = new Comment();
        comment.setContent(content);
        comment.setUserId(user.getId());
        comment.setHouseId(houseId);
        comment.setType(type);

        this.commonInsert(comment);

        return "redirect:/house/detail?id=" + houseId;
    }

    @RequestMapping("/comment/leaveBlogComment")
    public String leaveBlogComment(Integer blogId, Integer type, String content) {
        User user = UserContext.getUser();
        Comment comment = new Comment();
        comment.setContent(content);
        comment.setUserId(user.getId());
        comment.setBlogId(blogId);
        comment.setType(type);

        this.commonInsert(comment);

        return "redirect:/blog/detail?id=" + blogId;
    }

    private void commonInsert(Comment comment) {
        BeanHelper.setDefaultProp(comment, Comment.class);
        BeanHelper.onInsert(comment);
        commentService.addComment(comment);
    }

}
